package com.atguigu.spring.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;


public class LoginAspect {
	
	public void declareJoinPointExpression(){}
	
	public void beforMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		Object [] args = joinPoint.getArgs();
		System.out.println("The method " + methodName + " begins" + Arrays.asList(args));
	}
	public void afterMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();
		System.out.println("The method " + methodName + " after " + Arrays.asList(args));
	}
	
	public void afterReturnMethod(JoinPoint joinPoint, Object result){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method " + methodName + " result: " + result);
	}
	
	
	public void afterThrowingMethod(JoinPoint joinPoint, Exception ex){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("The method " + methodName + " occurs exception:with " + ex);
	}
	
	public Object aroundMethod(ProceedingJoinPoint pjd){
		Object result = null;
		
		Object[] args =  pjd.getArgs();
		String methodName = pjd.getSignature().getName();
		
		try {
			//前置通知
			System.out.println("--The method " + methodName + " begins with " + Arrays.asList(args));
			//执行目标方法
			result = pjd.proceed();
			//后置通知
			System.out.println("--The method ends with " + result);
			
		} catch (Throwable e) {
			//异常通知
			System.out.println("The method occurs exception " + e);
//			e.printStackTrace();
		}
		//返回通知
		System.out.println("The method " + methodName +"  ends ");
		
		return result ;
	}
}
